#!/bin/bash
#Ubuntu导入账号
if [[ -z "$(cat /root/m-net-tui 2>/dev/null)" ]];then
  echo -e "\e[31m/root/m-net-tui 账号列表为空,请填入账号\e[0m"
  echo -e "格式为:\e[31mVLAN 账号 密码 速率 网卡\e[0m"
  echo "网卡不填就是默认万兆网卡;账号后台网桥为br-if-XXXX的账号则使用XXXX为网卡不查询万兆"
  exit 1
fi
sed -i '/^$/d' /root/m-net-tui
sed -i '/^ /d' /root/m-net-tui
#获取万兆网卡
for item in $(grep -v -f <(ls -1 /sys/devices/virtual/net/) <(ls -1 /sys/class/net/) | grep -v 'veth'); do
    [[ "$(ethtool "${item}" | grep -c "10000Mb/s")" == "1" ]] && _nic_name="${item}"
done

if [[ -n "$(grep 'USERNAME=\|IP_MASK=' /root/m-net-tui)" ]]; then
    #旧表格格式 >>>转化为DCIM 表格格式
    rm -rf /root/import.json
    touch /root/import.json
    while read -r line; do
        ZHHAO="88:$(openssl rand -hex 5 | sed 's/\(..\)/\1:/g; s/.$//')"
        MACc=$(echo "$line" | awk -F= '{print $2}' | awk '{print $1}')
        SUlv=$(echo "$line" | awk -F= '{print $7}' | awk '{print $1}')
        WangQ=$(echo "$line" | awk -F= '{print $6}' | awk '{print $1}')
        if [[ -n "$(echo "$WangQ" | grep br-if)" ]]; then
            WangK="$(echo "$WangQ" | awk -F- '{print $3}')"
        else
            WangK=$_nic_name
        fi
        echo -e "$ZHHAO\t$WangK\t$MACc\t1\t$SUlv\t$SUlv\tNULL" >>/root/import.json
    done < /root/m-net-tui
    cat /root/import.json >/root/m-net-tui
elif [[ -z "$(grep 'NULL$' /root/m-net-tui)" ]]; then
    #VLAN 账号 密码 速率 网卡 >>>转化为DCIM 表格格式
    rm -rf /root/import.json
    touch /root/import.json
    while read -r line; do
        ZHHAO=$(echo "$line" | awk '{OFS="\t";print $2,$3,$1}')
        SUlv=$(echo "$line" | awk '{print $4}')
        WangK=$(echo "$line" | awk '{print $5}')
        [[ -z "$WangK" ]] && WangK=$_nic_name
        MACc=$(openssl rand -hex 5 | sed -e 's/^\(..\)\(..\)\(..\)\(..\)\(..\).*$/88:\1:\2:\3:\4:\5/')
        echo -e "$ZHHAO\t$WangK\t$MACc\t1\t$SUlv\t$SUlv\tNULL" >>/root/import.json
    done < /root/m-net-tui
    cat /root/import.json >/root/m-net-tui
fi
cat /root/m-net-tui
echo -e "\e[31m检查账号是否正常3秒后生成json文件导入\e[0m"
sleep 3
if [[ -n "$(grep 'NULL$' /root/m-net-tui)" ]] && [[ -n "$(grep '/' /root/m-net-tui)" ]]; then
    #static json格式转换
    echo "删除账号"
    rm -rf /root/import.json
    curl -s -X DELETE 127.0.0.1:41100/api/v2/network/static/data >/dev/null 2>&1
    curl -s -X POST 127.0.0.1:41100/api/v2/network/static/apply >/dev/null 2>&1
    sleep 3
    echo "开始导入"
    while read -r line; do
        MAC=$(echo $line | awk '{print $5}' | cut -d '=' -f 2)
        VLAN=$(echo $line | awk '{print $3}' | cut -d '=' -f 2)
        IP_MASK=$(echo $line | awk '{print $1}' | cut -d '=' -f 2)
        GATEWAY=$(echo $line | awk '{print $2}' | cut -d '=' -f 2)
        MAX_TX=$(echo $line | awk '{print $7}' | cut -d '=' -f 2)
        MAX_RX=$(echo $line | awk '{print $8}' | cut -d '=' -f 2)
        Pnic=$(echo $line | awk '{print $4}' | cut -d '=' -f 2)
        json_data=$(
                jq -n \
                --arg pnic "$Pnic" \
                --arg mac "$MAC" \
                --arg vlan "$VLAN" \
                --arg upload "$MAX_RX" \
                --arg download "$MAX_TX" \
                --arg ip_mask "$IP_MASK" \
                --arg gateway "$GATEWAY" \
                '{
                data: [
                    {
                        enable: true,
                        pnic: $pnic,
                        mac: $mac,
                        vlan: ($vlan | tonumber),
                        upload: ($upload | tonumber),
                        download: ($download | tonumber),
                        comment: "",
                        ip_mask: $ip_mask,
                        gateway: $gateway
                    }
                ],
            }'
        )
        echo -e "$json_data" >>/root/import.json
        curl -s -X POST 127.0.0.1:41100/api/v2/network/static/crud -H "Content-Type: application/json" -d "$json_data" >/dev/null 2>&1
    done < /root/m-net-tui
    curl -s -X POST 127.0.0.1:41100/api/v2/network/static/apply
    echo "应用账号"
elif [[ -n "$(grep 'NULL$' /root/m-net-tui)" ]]; then
    #PPPoE json格式转换
    echo "删除账号"
    rm -rf /root/import.json
    curl -s -X DELETE 127.0.0.1:41100/api/v2/network/pppoe/data >/dev/null 2>&1
    curl -s -X POST 127.0.0.1:41100/api/v2/network/pppoe/apply >/dev/null 2>&1
    sleep 3
    echo "开始导入"
    while read -r line; do
        MAC=$(echo $line | awk '{print $5}' | cut -d '=' -f 2)
        VLAN=$(echo $line | awk '{print $3}' | cut -d '=' -f 2)
        username=$(echo $line | awk '{print $1}' | cut -d '=' -f 2)
        password=$(echo $line | awk '{print $2}' | cut -d '=' -f 2)
        MAX_TX=$(echo $line | awk '{print $7}' | cut -d '=' -f 2)
        MAX_RX=$(echo $line | awk '{print $8}' | cut -d '=' -f 2)
        Pnic=$(echo $line | awk '{print $4}' | cut -d '=' -f 2)
        json_data=$(
                jq -n \
                --arg pnic "$Pnic" \
                --arg mac "$MAC" \
                --arg vlan "$VLAN" \
                --arg upload "$MAX_RX" \
                --arg download "$MAX_TX" \
                --arg username "$username" \
                --arg password "$password" \
                '{
                data: [
                    {
                        enable: true,
                        pnic: $pnic,
                        mac: $mac,
                        vlan: ($vlan | tonumber),
                        upload: ($upload | tonumber),
                        download: ($download | tonumber),
                        comment: "",
                        username: $username,
                        password: $password
                    }
                ],
            }'
        )
        echo -e "$json_data" >>/root/import.json
        curl -s -X POST 127.0.0.1:41100/api/v2/network/pppoe/crud -H "Content-Type: application/json" -d "$json_data" >/dev/null 2>&1
    done < /root/m-net-tui
    curl -s -X POST 127.0.0.1:41100/api/v2/network/pppoe/apply
    echo "应用账号"